package com.pro.service.imple;

import com.pro.dao.UserCouponMapper;
import com.pro.dao.UserCouponUsedMapper;
import com.pro.entity.UserCoupon;
import com.pro.entity.UserCouponUsed;
import com.pro.entity.UserCouponUsedPlus;
import com.pro.service.UserCouponUsedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

/**
 * @ClassName: UserCouonUsedServiceImple
 * @Author: 笑笑
 * @DATE: 2021/11/23
 * @TIME: 下午 07:18
 */
@Service
public class UserCouponUsedServiceImple implements UserCouponUsedService {
    @Resource
    private UserCouponUsedMapper userCouponUsedMapper;
    @Resource
    private UserCouponMapper userCouponMapper;

    //查询所有优惠券使用记录
    @Override
    public List<UserCouponUsedPlus> getAllTable(UserCouponUsedPlus userCouponUsedPlus) {
        return userCouponUsedMapper.getAllTable(userCouponUsedPlus);
    }

    //添加优惠券使用记录
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addUserCouponUsed(UserCouponUsedPlus userCouponUsedPlus, UserCoupon userCoupon) throws Exception {
        userCouponUsedPlus.setState(1);
        userCouponUsedPlus.setGetDate(String.valueOf(new Timestamp(new Date().getTime())));
        Boolean aBoolean = userCouponUsedMapper.addUserCouponUsed(userCouponUsedPlus);
        Boolean b = null;
        if (aBoolean){
            userCoupon.setUpdateDate(new Date());
            b = userCouponMapper.updateUserCoupon(userCoupon);
        }else{
            throw new Exception("添加失败");
        }
        return b;
    }
}
